package com.coding.controller;



import com.coding.dto.RestBean;
import com.coding.util.ImageCodeUtils;
import com.coding.util.RandomUtil;
import com.coding.util.ResponseDTO;
import com.coding.util.SendSms;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
//@CrossOrigin
@RestController
public class CodeController {

    @Autowired
    private RedisTemplate redisTemplate; // 自动注入StringRedisTemplate实例

    @Autowired
    private JavaMailSender mailSender;

    //	获得发件人信息
    @Value("${spring.mail.username}")
    private String from;

    // 手机验证码发送接口
    @RequestMapping("/phonecode")
        public RestBean<String> captcha(String phone) throws IOException, ExecutionException, InterruptedException {
        System.out.println("phone1:"+phone);
        // 发送短信并获取验证码
        String code = SendSms.sendSMS(phone);

        // 将验证码存储到Redis，键为"code_" + 手机号，值为验证码，过期时间为1000秒
        System.out.println("phone2:"+phone);
        redisTemplate.opsForValue().set("code_" + phone, code, 1000, TimeUnit.SECONDS);
        return RestBean.success("成功");
    }

    /**
     * 发送邮件验证码
     * @param email 发送的邮箱
     * @return
     */
    @PostMapping("/sendmailCode")
    public ResponseDTO save(@RequestParam String email) {

        // 获取随机6位验证码
        String code = RandomUtil.getSixBitRandom();

        SimpleMailMessage message = new SimpleMailMessage();
        // 发送邮箱
        message.setFrom(from);
        // 接受邮箱
        message.setTo(email);
        // 标题
        message.setSubject("测试服务平台-邮箱验证");
        // 内容
        message.setText("尊敬的用户您好：" +
                "您正在进行邮箱验证，本次验证码为:" + code + "，请在3分钟内进行使用。" +
                "如非本人操作，请忽略此邮件，由此给您带来的不便请谅解！--测试服务平台");
        try{
            // 发送邮件
            mailSender.send(message);
            redisTemplate.opsForValue().set("emailcode" + email, code, 1000, TimeUnit.SECONDS);
            return ResponseDTO.success("验证码发送成功");
        } catch (Exception e) {
            return ResponseDTO.success(404,"验证码系统发送失败，请检查邮箱账号是否正确！",e);
        }
    }


    //验证码
    @RequestMapping("/code")
    public void captcha(HttpServletResponse response,@RequestParam("flag") String flag) throws IOException{

        System.out.println("flag---------"+flag);
        response.setHeader("Expires","-1");
        response.setHeader("Cache-Control","no-cache");
        response.setHeader("Pragma","-1");
        ImageCodeUtils vCode = new ImageCodeUtils(116,36,5,10);
        String code = vCode.getCode();
        redisTemplate.opsForValue().set("code" + flag, code, 1000, TimeUnit.SECONDS);
//        session.setAttribute("code",code);
        System.out.println("你要的验证码是"+code);
        vCode.write(response.getOutputStream());
    }


}
